package com.bk.cyxf.lf;

import com.bk.cyxf.log.LogManager;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.object.IObject;
import jt.classic.system.object.ObjectLCManager;
import jt.classic.system.object.ObjectManager;
import jt.classic.system.purview.PurviewManager;
import jt.classic.system.role.IRole;
import jt.classic.system.role.RoleManager;
import jt.classic.system.user.IUser;
import jt.classic.system.user.UserManager;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.limp.basework.SimpleBean4Table;
import org.limp.basework.Table;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;

import org.limp.mine.DataTrimmerI;
import org.limp.mine.DateTrimmer;
import org.limp.mine.StringTool;

import java.sql.Connection;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 来访办理
 *
 * @author lvt
 *
 */
public class LfblAction extends SimpleAction {
    /**
     * 来访办理
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;
        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        String querykey = StringTool.checkString(request.getParameter(
                    "querykey"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来访登记信息并不存在");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LF");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String status = StringTool.checkString(info.get("BLSTATUS"));

                if (!status.equals("0")) {
                    /*已办理事项,不能再次办理*/
                    request.setAttribute("error", "来访登记已在办理过程中,不能重复办理");
                    forward = mapping.findForward("error");
                } else {
                    request.setAttribute("querykey", querykey);
                    request.setAttribute("object", object);
                    request.setAttribute("wobject", object.iobject());
                    forward = mapping.findForward("lfbl");
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来访审批
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward editsp(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来访登记访息并不存在");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LF");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String status = StringTool.checkString(info.get("SHSTATUS"));

                if (status.equals("2")) {
                    /*已办理事项,不能再次办理*/
                    request.setAttribute("error", "来访登记已办结,不能重复审批");
                    forward = mapping.findForward("error");
                } else {
                    request.setAttribute("object", object);
                    request.setAttribute("wobject", object.iobject());
                    forward = mapping.findForward("lxsp");
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来访派遣办理
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward deal(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        String status = StringTool.checkString(request.getParameter("BLSTATUS"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectManager manager = new ObjectManager(conn);

            ObjectLCManager lcmanager = new ObjectLCManager(conn);

            IObject object = lcmanager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("result",
                    "{success: false, errCode : '-1'}");
            } else {
                IUser user = ISystem.getSessionUser(request);

                if (manager.purview(id, user) < 2) {
                    request.setAttribute("result",
                        "{success: false, errCode : '-2'}");
                } else {
                    Table tbl = object.iobject().itable("APP_LF");

                    Map info = (Map) ((List) object.icontent(tbl)).get(0);

                    String lfid = StringTool.checkString(info.get("LFID"));

                    /*原办理状态*/
                    String orgsts = StringTool.checkString(info.get("BLSTATUS"));

                    /*来访类型*/
                    String LFTYPE = StringTool.checkString(info.get("LFTYPE"));

                    if (!orgsts.equals("0")) {
                        /*已办理事项,不能再次办理*/
                        request.setAttribute("result",
                            "{success: false, errCode : '-3'}");
                    } else {
                        /*自处理*/
                        String stsLabel = "自处理";

                        if (status.equals("2")) {
                            /*办理*/
                            stsLabel = "派遣办理";
                        } else if (status.equals("3")) {
                            /*上报*/
                            stsLabel = "上报处理";
                        } else if (status.equals("-1")) {
                            /*不予办理*/
                            stsLabel = "不予办理";
                        }

                        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

                        SimpleBean4Table sbt = new SimpleBean4TableImpl("APP_LF",
                                cbh);

                        sbt.getResource().put("lfid", lfid);

                        sbt.getResource().put("PQRID", user.id());
                        sbt.getResource().put("PQRNAME", user.iname());
                        sbt.getResource().put("PQRGROUPID", user.igroup().id());
                        sbt.getResource().put("PQRGROUPNAME",
                            user.igroup().iname());

                        if (status.equals("-1") || status.equals("1")) {
                            sbt.getResource().put("LFSTATUS", "1");

                            String backInfo = StringTool.checkString(request.getParameter(
                                        "H_BACKINFO"));

                            /*信访办办理*/
                            SimpleBean4Table sbt1 = new SimpleBean4TableImpl(
                                    "APP_XFHANDLER");
                            sbt1.getResource().put("H_GROUPID",
                                user.igroup().id());
                            sbt1.getResource().put("H_GROUPNAME",
                                user.igroup().iname());
                            sbt1.getResource().put("XFID", lfid);
                            sbt1.getResource().put("XF_TYPE", "1");
                            sbt1.getResource().put("H_USERID", user.id());
                            sbt1.getResource().put("H_USERNAME", user.iname());
                            sbt1.getResource().put("H_STATUS", "1");
                            sbt1.getResource().put("H_BACKINFO", backInfo);
                            sbt1.getResource().put("H_DATE",
                                DateTrimmer.getYMDHMS());

                            BaseworkUtil util = new BaseworkUtil();
                            Map m = util.manualSave1(sbt1, conn);

                            manager.createSystemRelationObject(id,
                                "APP_XFHANDLER", (String) m.get("HID"));
                        } else if (status.equals("2")) {
                            String jbrgroupid = StringTool.checkString(request.getParameter(
                                        "H_GROUPID"));

                            String jbrgroupname = StringTool.checkString(request.getParameter(
                                        "H_GROUPNAME"));
                            
                            String[] idArray = jbrgroupid.split(",");
                            String[] nameArray = jbrgroupname.split(",");
                            
                            
                            BaseworkUtil util = new BaseworkUtil();
                            PurviewManager pm = new PurviewManager(conn);
                            RoleManager rm = new RoleManager(conn);
                            
                            for(int i= 0;i <idArray.length;i++){
                            	
	                            /*派遣科室办理*/
	                            SimpleBean4Table sbt1 = new SimpleBean4TableImpl(
	                                    "APP_XFHANDLER");
	
	                            /*添加处理科室ID*/
	                            sbt1.getResource().put("H_GROUPID", idArray[i]);
	
	                            /*添加处理科室名称*/
	                            sbt1.getResource().put("H_GROUPNAME", nameArray[i]);
	
	                            sbt1.getResource().put("XFID", lfid);
	                            sbt1.getResource().put("XF_TYPE", "1");
	                            sbt1.getResource().put("H_STATUS", "0");
	
	                            
	                            Map m = util.manualSave1(sbt1, conn);
	
	                            manager.createSystemRelationObject(id,
	                                "APP_XFHANDLER", (String) m.get("HID"));
	
	                            
                            }
                            
                            IRole role = null;
                        	
                            /*街乡信访办理负责人*/
                            if (LFTYPE.equals("0")) {
                                role = rm.getRoleByKey("jxpic");
                            }
                            /*区信访办理负责人*/
                            else if (LFTYPE.equals("1")) {
                                role = rm.getRoleByKey("qupic");
                            }

                            /*授权信访办理负责人可对来信进行办理*/
                            pm.insert(object.id(), object.iname(),
                                object.ikey(), "ISYS_OBJECT", 3, role.id(),
                                role.iname(), "role", user, false);
                        }

                        BaseworkUtil util = new BaseworkUtil();

                        util.manualUpdate1(sbt, conn);

                        LogManager lm = new LogManager(conn);

                        lm.log(user, lfid, "1", "lfcz", stsLabel);

                        /*上报*/
                        if (status.equals("3")) {
                        }

                        conn.commit();

                        request.setAttribute("result", "{success: true}");
                    }
                }
            }
        } catch (Exception ex) {
            conn.rollback();

            request.setAttribute("result", "{success: false, errCode : '-3'}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 来访办理
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward edithdl(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来访登记信息并不存在");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LF");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String lfid = StringTool.checkString(info.get("LFID"));

                String status = StringTool.checkString(info.get("LFSTATUS"));

                if (status.equals("1")) {
                    /*已办理事项,不能再次办理*/
                    request.setAttribute("error", "来访登记已办结,不能重复办理");
                    forward = mapping.findForward("error");
                } else {
//                    String sql = "SELECT * FROM APP_XFHANDLER WHERE XFID = ? AND XF_TYPE = ?";
//
//                    DataTrimmerI trimmer = new DataTrimmerI(conn);
//
//                    DataTrimmerI.DataItemList dil = trimmer.createDIL();
//                    dil.set(lfid);
//                    dil.set("1");

//                    Map hdlMap = trimmer.searchSingleData(sql, dil);
//
//                    String hdluserid = StringTool.checkString(hdlMap.get(
//                                "H_USERID"));
//
//                    IUser user = ISystem.getSessionUser(request);

//                    if (!hdluserid.equals("") && !hdluserid.equals(user.id())) {
//                        request.setAttribute("error", "来访已在办理过程中...");
//                        forward = mapping.findForward("error");
//                    } else {
//                        if (hdluserid.equals("")) {
//                            String hdlkey = StringTool.checkString(hdlMap.get(
//                                        "HID"));
//
//                            SimpleBean4Table sbt1 = new SimpleBean4TableImpl(
//                                    "APP_XFHANDLER");
//
//                            /*设置办理人ID*/
//                            sbt1.getResource().put("H_USERID", user.id());
//
//                            /*设置办理人姓名*/
//                            sbt1.getResource().put("H_USERNAME", user.iname());
//
//                            sbt1.getResource().put("HID", hdlkey);
//
//                            BaseworkUtil util = new BaseworkUtil();
//
//                            Map m = util.manualUpdate1(sbt1, conn);
//
//                            conn.commit();
//                        }

                        request.setAttribute("object", object);
                        request.setAttribute("wobject", object.iobject());
                        forward = mapping.findForward("lfhdl");
//                    }
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来访科室办理
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward handle(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;
        CommonBean4HSQ cbh = new CommonBean4HSQ(request);
        Map param = cbh.getResource();
        String id = (String)param.get("id");
        String backInfo = (String)param.get("H_BACKINFO");
        
//        String id = StringTool.checkString(request.getParameter("id"));
//
//        String backInfo = StringTool.checkString(request.getParameter("H_BACKINFO"));
        
//        String clfsid = StringTool.checkString(request.getParameter("CLFSID"));
//
//        String clfs = StringTool.checkString(request.getParameter("CLFS"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectManager manager = new ObjectManager(conn);

            ObjectLCManager lcmanager = new ObjectLCManager(conn);

            IObject object = lcmanager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("result",
                    "{success: false, errCode : '-1'}");
            } else {
                IUser user = ISystem.getSessionUser(request);

                if (manager.purview(id, user) < 2) {
                    request.setAttribute("result",
                        "{success: false, errCode : '-2'}");
                } else {
                    Table tbl = object.iobject().itable("APP_LF");
                    Map info = (Map) ((List) object.icontent(tbl)).get(0);
                    String lfid = StringTool.checkString(info.get("LFID"));
                    String orgsts = StringTool.checkString(info.get("LFSTATUS"));
                    String pqrgroupid = StringTool.checkString(info.get("PQRGROUPID"));
                    

                    if (orgsts.equals("1")) {
                        /*已审批事项,不能再次审批*/
                        request.setAttribute("result",
                            "{success: false, errCode : '-3'}");
                    } else {
                        String sql = "SELECT HID FROM APP_XFHANDLER WHERE XFID = ? AND XF_TYPE = ? AND H_GROUPID = ?";

                        DataTrimmerI trimmer = new DataTrimmerI(conn);

                        DataTrimmerI.DataItemList dil = trimmer.createDIL();

                        dil.set(lfid);
                        dil.set(1);
                        dil.set(user.igroup().id());

                        Map hdlMap = trimmer.searchSingleData(sql, dil);

                        String hdlkey = StringTool.checkString(hdlMap.get("HID"));
                        
                        SimpleBean4Table sbt = new SimpleBean4TableImpl(
                        "APP_XFHANDLER", cbh);

		                sbt.getResource().put("H_STATUS", "1");
		                sbt.getResource().put("H_BACKINFO", backInfo);
		                sbt.getResource().put("H_DATE", DateTrimmer.getYMDHMS());
		                sbt.getResource().put("HID", hdlkey);
		                sbt.getResource().put("H_USERID", user.id());
		                sbt.getResource().put("H_USERNAME", user.iname());
		
		                BaseworkUtil util = new BaseworkUtil();
                        //如果是派遣人科室（一般是信访办）需要判断其他科室是否已经办理完成，如果是其他科室则直接保存办理结果
                        if( user.igroup().id().equals(pqrgroupid)){
                        	//暂时不对派遣办理的科室办理情况作判断-----start
                           /*
                        	//判断派遣办理的科室是否办理完成
                            String sql_2 = "SELECT * FROM APP_XFHANDLER WHERE XFID = ? AND XF_TYPE = ? AND H_GROUPID != ?";
                            DataTrimmerI.DataItemList dil_2 = trimmer.createDIL();
                            dil_2.set(lfid);
                            dil_2.set("1");
                            dil_2.set(user.igroup().id());	
                            List<Map> handlerList = trimmer.searchMultiData(sql_2, dil_2);
                            boolean handlerResult = true;
                            for(Map m: handlerList){
                            	String h_status = StringTool.checkString(m.get("H_STATUS"));
                            	if("0".equals(h_status)){
                            		//只要有一个科室为办理完成则该条来访记录的科室办理环节未完成
                            		handlerResult = false;
                            	}
                            }
                            
                            //其他科室都办理完成并且当前用户所在科室与派遣人科室一致，由该科室结束办理
                            if(handlerResult){
                            	util.manualUpdate1(sbt, conn);
                            	
                                SimpleBean4Table sb = new SimpleBean4TableImpl("APP_LF");

                                sb.getResource().put("LFSTATUS", "1");

                                sb.getResource().put("lfid", lfid);

                                util.manualUpdate1(sb, conn);
                                request.setAttribute("result", "{success: true}");
                            }else{
                            	//返回错误信息
                            	request.setAttribute("result", "{success: \"false\",msg:\"经办部门未办理完\"'}");
                            }*/
//                        	暂时不对派遣办理的科室办理情况作判断-----end
                        	util.manualUpdate1(sbt, conn);
                        	
                            SimpleBean4Table sb = new SimpleBean4TableImpl("APP_LF");

                            sb.getResource().put("LFSTATUS", "1");

                            sb.getResource().put("lfid", lfid);

                            util.manualUpdate1(sb, conn);
                            request.setAttribute("result", "{success: true}");
                        
                        	
                        }else{
                        	util.manualUpdate1(sbt, conn);
                        	request.setAttribute("result", "{success: true}");
                        }



                        LogManager lm = new LogManager(conn);

                        lm.log(user, lfid, "1", "lfhandle", "");

                        conn.commit();

                        
                    }
                }
            }

            forward = mapping.findForward("result");
        } catch (Exception ex) {
            conn.rollback();

            request.setAttribute("result", "{success: false, errCode : '-4'}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来访复查
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward editfc(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来访登记信息并不存在");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LF");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String lxid = StringTool.checkString(info.get("LFID"));

                String status = StringTool.checkString(info.get("FCSTATUS"));

                if (!status.equals("-1")) {
                    /*已办理事项,不能再次办理*/
                    request.setAttribute("error", "不能重复进行复查办理");
                    forward = mapping.findForward("error");
                } else {
                    request.setAttribute("object", object);
                    request.setAttribute("wobject", object.iobject());
                    forward = mapping.findForward("lffc");
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来访复查派遣
     * <p>
     *    安排来访复查办理人员
     * </p>
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward savefc(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectManager manager = new ObjectManager(conn);

            ObjectLCManager lcmanager = new ObjectLCManager(conn);

            IObject object = lcmanager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("result",
                    "{success: false, errCode : '-1'}");
            } else {
                IUser user = ISystem.getSessionUser(request);

                if (manager.purview(id, user) < 2) {
                    request.setAttribute("result",
                        "{success: false, errCode : '-2'}");
                } else {
                    Table tbl = object.iobject().itable("APP_LF");

                    Map info = (Map) ((List) object.icontent(tbl)).get(0);

                    String lfid = StringTool.checkString(info.get("LFID"));

                    String orgsts = StringTool.checkString(info.get("FCSTATUS"));

                    if (orgsts.equals("0")) {
                        /*已复查事项,不能再次复查*/
                        request.setAttribute("result",
                            "{success: false, errCode : '-3'}");
                    } else {
                        String fcUserId = StringTool.checkString(request.getParameter(
                                    "FC_USERID"));

                        String fcBLDate = StringTool.checkString(request.getParameter(
                                    "FC_DATE"));

                        UserManager um = new UserManager(conn);

                        /*复查办理人*/
                        IUser fcUser = um.getUserById(fcUserId);

                        SimpleBean4Table sbt = new SimpleBean4TableImpl(
                                "APP_XFFC");

                        sbt.getResource().put("XFID", lfid);
                        sbt.getResource().put("XF_TYPE", "1");
                        sbt.getResource().put("FC_STATUS", "0");
                        sbt.getResource().put("FC_USERID", fcUser.id());
                        sbt.getResource().put("FC_USERNAME", fcUser.iname());
                        sbt.getResource().put("FC_GROUPID", fcUser.igroup().id());
                        sbt.getResource().put("FC_GROUPNAME",
                            fcUser.igroup().iname());
                        sbt.getResource().put("FC_DATE", fcBLDate);

                        BaseworkUtil util = new BaseworkUtil();

                        Map fcMap = util.manualSave1(sbt, conn);

                        String foid = (String) fcMap.get("FCID");

                        ObjectManager om = new ObjectManager(conn);
                        om.createSystemRelationObject(id, "APP_XFFC", foid);

                        SimpleBean4Table sb = new SimpleBean4TableImpl("APP_LF");

                        sb.getResource().put("LFSTATUS", "0");

                        sb.getResource().put("FCSTATUS", "0");

                        sb.getResource().put("lfid", lfid);

                        util.manualUpdate1(sb, conn);

                        PurviewManager pm = new PurviewManager(conn);

                        /*授权信访复查负责人可对来信进行复查办理*/
                        pm.insert(object.id(), object.iname(), object.ikey(),
                            "ISYS_OBJECT", 3, fcUser.id(), fcUser.iname(),
                            "uesr", user, false);

                        LogManager lm = new LogManager(conn);

                        lm.log(user, lfid, "0", "lffccz", "");

                        conn.commit();

                        request.setAttribute("result", "{success: true}");
                    }
                }
            }

            forward = mapping.findForward("result");
        } catch (Exception ex) {
            conn.rollback();

            request.setAttribute("result", "{success: false, errCode : '-4'}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来访复查办理
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward blfc(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            ObjectManager om = new ObjectManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来访登记信息并不存在");

                forward = mapping.findForward("error");
            } else if (om.purview(id, user) < 2) {
                request.setAttribute("error", "您没有复查办理权限,无法进行复查办理");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LF");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String lxid = StringTool.checkString(info.get("LFID"));

                String status = StringTool.checkString(info.get("FCSTATUS"));

                if (status.equals("-1")) {
                    request.setAttribute("error", "该来访不能进行复查办理");

                    forward = mapping.findForward("error");
                } else if (status.equals("1")) {
                    request.setAttribute("error", "来访已经复查办理结束,不能重复复查");

                    forward = mapping.findForward("error");
                } else {
                    request.setAttribute("object", object);
                    request.setAttribute("wobject", object.iobject());
                    forward = mapping.findForward("lffcbl");
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来访复查办理
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward saveblfc(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        String id = StringTool.checkString(cbh.getResource().get("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectManager manager = new ObjectManager(conn);

            ObjectLCManager lcmanager = new ObjectLCManager(conn);

            IObject object = lcmanager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("result",
                    "{success: false, errCode : '-1'}");
            } else {
                IUser user = ISystem.getSessionUser(request);

                if (manager.purview(id, user) < 2) {
                    request.setAttribute("result",
                        "{success: false, errCode : '-2'}");
                } else {
                    Table tbl = object.iobject().itable("APP_LF");

                    Map info = (Map) ((List) object.icontent(tbl)).get(0);

                    String lfid = StringTool.checkString(info.get("LFID"));

                    String orgsts = StringTool.checkString(info.get("FCSTATUS"));

                    if (orgsts.equals("1")) {
                        /*已办理复查事项,不能重复复查*/
                        request.setAttribute("result",
                            "{success: false, errCode : '-3'}");
                    } else {
                        Table table = object.iobject().itable("APP_XFFC");

                        Map fcInfo = (Map) ((List) object.icontent(table)).get(0);

                        String fcid = StringTool.checkString(fcInfo.get("FCID"));

                        SimpleBean4Table sbt = new SimpleBean4TableImpl("APP_XFFC",
                                cbh);

                        sbt.getResource().put("FCID", fcid);
                        sbt.getResource().put("FC_STATUS", "1");
                        sbt.getResource().put("FC_BLDATE",
                            DateTrimmer.getYMDHMS());

                        BaseworkUtil util = new BaseworkUtil();

                        Map fcMap = util.manualUpdate1(sbt, conn);

                        SimpleBean4Table sb = new SimpleBean4TableImpl("APP_LF");

                        sb.getResource().put("LFSTATUS", "1");

                        sb.getResource().put("FCSTATUS", "1");

                        sb.getResource().put("lfid", lfid);

                        util.manualUpdate1(sb, conn);

                        LogManager lm = new LogManager(conn);

                        lm.log(user, lfid, "0", "lffcbl", "");

                        conn.commit();

                        request.setAttribute("result", "{success: true}");
                    }
                }
            }

            forward = mapping.findForward("result");
        } catch (Exception ex) {
            conn.rollback();

            request.setAttribute("result", "{success: false, errCode : '-4'}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来访复核
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward editfh(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来访登记信息并不存在");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LF");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String lfid = StringTool.checkString(info.get("LFID"));

                String status = StringTool.checkString(info.get("FHSTATUS"));

                if (!status.equals("-1")) {
                    /*已办理事项,不能再次办理*/
                    request.setAttribute("error", "不能重复进行复核办理");
                    forward = mapping.findForward("error");
                } else {
                    request.setAttribute("object", object);
                    request.setAttribute("wobject", object.iobject());
                    forward = mapping.findForward("lffh");
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来访复核派遣
     * <p>
     *    安排来访复核办理人员
     * </p>
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward savefh(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectManager manager = new ObjectManager(conn);

            ObjectLCManager lcmanager = new ObjectLCManager(conn);

            IObject object = lcmanager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("result",
                    "{success: false, errCode : '-1'}");
            } else {
                IUser user = ISystem.getSessionUser(request);

                if (manager.purview(id, user) < 2) {
                    request.setAttribute("result",
                        "{success: false, errCode : '-2'}");
                } else {
                    Table tbl = object.iobject().itable("APP_LF");

                    Map info = (Map) ((List) object.icontent(tbl)).get(0);

                    String lfid = StringTool.checkString(info.get("LFID"));

                    String orgsts = StringTool.checkString(info.get("FHSTATUS"));

                    if (orgsts.equals("0")) {
                        /*已复核事项,不能再次复核*/
                        request.setAttribute("result",
                            "{success: false, errCode : '-3'}");
                    } else {
                        String fhUserId = StringTool.checkString(request.getParameter(
                                    "FH_USERID"));

                        String fhBLDate = StringTool.checkString(request.getParameter(
                                    "FH_DATE"));

                        UserManager um = new UserManager(conn);

                        /*复核办理人*/
                        IUser fcUser = um.getUserById(fhUserId);

                        SimpleBean4Table sbt = new SimpleBean4TableImpl(
                                "APP_XFFH");

                        sbt.getResource().put("XFID", lfid);
                        sbt.getResource().put("XF_TYPE", "1");
                        sbt.getResource().put("FH_STATUS", "0");
                        sbt.getResource().put("FH_USERID", fcUser.id());
                        sbt.getResource().put("FH_USERNAME", fcUser.iname());
                        sbt.getResource().put("FH_GROUPID", fcUser.igroup().id());
                        sbt.getResource().put("FH_GROUPNAME",
                            fcUser.igroup().iname());
                        sbt.getResource().put("FH_DATE", fhBLDate);

                        BaseworkUtil util = new BaseworkUtil();

                        Map fcMap = util.manualSave1(sbt, conn);

                        String foid = (String) fcMap.get("FHID");

                        ObjectManager om = new ObjectManager(conn);
                        om.createSystemRelationObject(id, "APP_XFFH", foid);

                        SimpleBean4Table sb = new SimpleBean4TableImpl("APP_LF");

                        sb.getResource().put("LFSTATUS", "0");

                        sb.getResource().put("FHSTATUS", "0");

                        sb.getResource().put("lfid", lfid);

                        util.manualUpdate1(sb, conn);

                        PurviewManager pm = new PurviewManager(conn);

                        /*授权信访复查负责人可对来访进行复查办理*/
                        pm.insert(object.id(), object.iname(), object.ikey(),
                            "ISYS_OBJECT", 3, fcUser.id(), fcUser.iname(),
                            "uesr", user, false);

                        LogManager lm = new LogManager(conn);

                        lm.log(user, lfid, "0", "lffhcz", "");

                        conn.commit();

                        request.setAttribute("result", "{success: true}");
                    }
                }
            }

            forward = mapping.findForward("result");
        } catch (Exception ex) {
            conn.rollback();

            request.setAttribute("result", "{success: false, errCode : '-4'}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来访复核
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward blfh(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            ObjectManager om = new ObjectManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来访登记信息并不存在");

                forward = mapping.findForward("error");
            } else if (om.purview(id, user) < 2) {
                request.setAttribute("error", "您没有复核办理权限,无法进行复核办理");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LF");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String lxid = StringTool.checkString(info.get("LFID"));

                String status = StringTool.checkString(info.get("FHSTATUS"));

                if (status.equals("-1")) {
                    request.setAttribute("error", "该来访不能进行复核办理");

                    forward = mapping.findForward("error");
                } else if (status.equals("1")) {
                    request.setAttribute("error", "来访已经复核办理结束,不能重复复核");

                    forward = mapping.findForward("error");
                } else {
                    request.setAttribute("object", object);
                    request.setAttribute("wobject", object.iobject());
                    forward = mapping.findForward("lffhbl");
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来访复核办理
     * <p>
     *    安排来访复核办理人员
     * </p>
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward saveblfh(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        String id = StringTool.checkString(cbh.getResource().get("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectManager manager = new ObjectManager(conn);

            ObjectLCManager lcmanager = new ObjectLCManager(conn);

            IObject object = lcmanager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("result",
                    "{success: false, errCode : '-1'}");
            } else {
                IUser user = ISystem.getSessionUser(request);

                if (manager.purview(id, user) < 2) {
                    request.setAttribute("result",
                        "{success: false, errCode : '-2'}");
                } else {
                    Table tbl = object.iobject().itable("APP_LX");

                    Map info = (Map) ((List) object.icontent(tbl)).get(0);

                    String lxid = StringTool.checkString(info.get("LXID"));

                    String orgsts = StringTool.checkString(info.get("FHSTATUS"));

                    if (orgsts.equals("1")) {
                        /*已办理复核事项,不能重复复核*/
                        request.setAttribute("result",
                            "{success: false, errCode : '-3'}");
                    } else {
                        Table table = object.iobject().itable("APP_XFFH");

                        Map fhInfo = (Map) ((List) object.icontent(table)).get(0);

                        String fhid = StringTool.checkString(fhInfo.get("FHID"));

                        SimpleBean4Table sbt = new SimpleBean4TableImpl("APP_XFFH",
                                cbh);

                        sbt.getResource().put("FHID", fhid);
                        sbt.getResource().put("FH_STATUS", "1");
                        sbt.getResource().put("FH_BLDATE",
                            DateTrimmer.getYMDHMS());

                        BaseworkUtil util = new BaseworkUtil();

                        Map fhMap = util.manualUpdate1(sbt, conn);

                        SimpleBean4Table sb = new SimpleBean4TableImpl("APP_LX");

                        sb.getResource().put("LXSTATUS", "1");

                        sb.getResource().put("FHSTATUS", "1");

                        sb.getResource().put("lxid", lxid);

                        util.manualUpdate1(sb, conn);

                        LogManager lm = new LogManager(conn);

                        lm.log(user, lxid, "0", "lxfhbl", "");

                        conn.commit();

                        request.setAttribute("result", "{success: true}");
                    }
                }
            }

            forward = mapping.findForward("result");
        } catch (Exception ex) {
            conn.rollback();

            request.setAttribute("result", "{success: false, errCode : '-4'}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }
}
